package com.ljm.controller;/**
 * @author： li
 * @date： 2022-04-22 15:18
 * @version 1.0
 */
import com.ljm.bin.article;
import com.ljm.dao.articleDao;
import com.ljm.util.DBUtil1;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import javax.servlet.*;
import javax.servlet.annotation.WebServlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author： ljm
 * @date： 2022-04-22 15:18
 * @version 1.0
 */
@WebServlet("/getlist")
public class listServlet implements Servlet {
    articleDao dao = new articleDao();
    @Override
    public void init(ServletConfig servletConfig) throws ServletException {

    }

    @Override
    public ServletConfig getServletConfig() {
        return null;
    }

    @Override
    public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException {
        servletResponse.setContentType("text/json; charset=utf-8");
        PrintWriter out =servletResponse.getWriter();
        ArrayList<article> list = new ArrayList<>();
        try {
            list= (ArrayList<article>) articleDao.getArticle();
            JSONArray ja = new JSONArray();
            JSONObject jo = new JSONObject();
            for(article i :list){
            jo.put("title",i.getTitle());
            jo.put("content",i.getContent());
                jo.put("id",i.getId());
                jo.put("date",i.getDate());
                jo.put("img",i.getImg());
            ja.add(jo);
            }
            out = servletResponse.getWriter();
            out.println(ja);
        } catch (SQLException e) {
            out.println("ERROR!");
            e.printStackTrace();
        }
    }

    @Override
    public String getServletInfo() {
        return null;
    }

    @Override
    public void destroy() {

    }
}
